﻿using ConcreteService;
using Infrastructures;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

public class DisplayUtil
{
    public static bool HasException(Page page, string mode)
    {
        if (!mode.IsNullOrEmpty() && mode == "1")
        {
            page.ClientScript.RegisterStartupScript(typeof(Page), "HasException", "common.alertMessage('操作失败,请重新提交。');", true);
            return true;
        }

        return false;
    }

    public static void AlertMessage(Page page, string message)
    {
        page.ClientScript.RegisterStartupScript(typeof(Page), "AlertMessage", "common.alertMessage('" + message + "');", true);
    }

    public static void Loading(Page page, bool isStart = true, string crtl = "bodyContent")
    {
        if (isStart)
            page.ClientScript.RegisterStartupScript(typeof(Page), "Loading", "common.loading($('#" + crtl + "'))", true);
        else
            page.ClientScript.RegisterStartupScript(typeof(Page), "Loading", "common.loadComplete($('#" + crtl + "'))", true);
    }

    public static void ListAssociation(int id, Dictionary<string, string> dic, AssociationService assoservice = null, string type = "Creator")
    {
        if (assoservice == null)
            assoservice = ServiceFactory.CreateInstance<AssociationService>();

        Association asso = null;

        if (type == "Creator")
            asso = assoservice.GetAssociationByUserID(id);
        else
            asso = assoservice.GetAssociation(id);

        if (asso != null)
        {
            var dicobj = asso.ToDictionary();
            foreach (var item in dicobj)
            {
                if (dic.ContainsKey(item.Key))
                    continue;
                dic.Add(item.Key, item.Value);
            }
            dic.AddOrReplace("LogoPath", SystemConfig.Get("UploadHttpPath") + asso.LogoPath);
            School school = AppConfig.GetSchool(asso.SchoolNO);
            if (school != null)
            {
                dic.Add("City", school.City);
                dic.Add("SchoolName", school.SchoolName);
                //dic.Add("SchoolAddress", school.Address);
            }

        }
    }

    public static string GetPastDays(string date)
    {
        int days = date.GetPastDays();
        if (days == 0)
            return "最新发布";
        else if (days < 32)
            return days + "天前发布";
        else if (days > 32)
            return "一月前发布";

        return days + "天前发布";
    }

}
